/***************************************************************************
*  Copyleft (3) 2008 by joker                                              *
*  take-cha@cyric.tohoku.ac.jp                                             *
*                                                                          *
*  This software is provided to you "AS IS," and the authors and           *
*  contributors disclaim any warranty or liability obligations to you      *
*  of any kind, implied or express, including but not limited to the       *
*  warranties of merchantability, fitness for a particular purpose,        *
*  and non-infringement.                                                   *
***************************************************************************/
#define BINDIR "/usr/local/pem/bin"

#include "rpem3d.h"
#include <kapplication.h>
#include <kaboutdata.h>
#include <kcmdlineargs.h>
#include <klocale.h>

#include <qmessagebox.h>

//
#include <qtextcodec.h>

static const char description[] =
    I18N_NOOP ( "A KDE KPart Application" );

static const char version[] = "0.1";

static KCmdLineOptions options[] =
{
//    { "+[URL]", I18N_NOOP( "Document to open" ), 0 },
	KCmdLineLastOption
};

int main ( int argc, char **argv )
{
	KAboutData about ( "rpem3d", I18N_NOOP ( "rpem3d" ), version, description,
	                   KAboutData::License_GPL, "(C) 2008 joker", 0, 0, "joker@regulus.pet.cyric.tohoku.ac.jp" );
	about.addAuthor ( "joker", 0, "joker@regulus.pet.cyric.tohoku.ac.jp" );
	KCmdLineArgs::init ( argc, argv, &about );
	KCmdLineArgs::addCmdLineOptions ( options );
	KApplication app;
	rPem3D *mainWin = 0;

//    if (app.isRestored())
//    {
//        RESTORE(rpem3d);
//    }
//    else
//    {
	// no session.. just start up normally
	KCmdLineArgs *args = KCmdLineArgs::parsedArgs();

	/// @todo do something with the command line args here
	{
		static QMessageBox* bye = new QMessageBox;
		char com[1024];
		char str[512],*ptr;
		int l;
		FILE *f;
		strcpy ( com,"ps ax|grep " );
		strcat ( com,BINDIR );
		strcat ( com,"/\\[" );
		l=strlen(com);
		memcpy ( com+l,argv[0],1 );
		com[l+2]='\0';
		strcat ( com,"\\]" );
		strcat ( com,argv[0]+1 );
		strcat ( com,"| wc -l" );
		f=popen ( com,"r" );
		if ( f==0 )
		{
			bye->critical ( 0,"(T_T)","*** FATAL ERROR ***\nOverload detection failed!","close" );
			exit ( 1 );
		}
		else
		{
			fgets ( str,512,f );
			ptr=strchr ( str,'\n' );
			if ( ptr!=0 ) *ptr='\0';
			if ( atoi ( str )>=2 )
			{
				bye->critical ( 0,"(T_T)","*** FATAL ERROR ***\nAlready running!","close" );
				exit ( 1 );
			}
			pclose ( f );
		}
	}
	{
		char me[512];
		bool f;
		strcpy ( me,BINDIR );
		strcat ( me,"/" );
		strcat ( me,argv[0] );
strcpy(me,"/home/pem");//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 
		if ( open_shm ( me, &f ) !=0 )
		{
			static QMessageBox* bye = new QMessageBox;
			bye->critical ( 0,"(T_T)","*** FATAL ERROR ***\nCan not allocate memory","close" );
			exit ( 1 );
		}
		else
		{
			/// default text code
//			QTextCodec* codec = QTextCodec::codecForName ( "sjis" );
//			QTextCodec* codec = QTextCodec::codecForName ( "utf16" );
			QTextCodec* codec = QTextCodec::codecForName ( "eucJP" );
			app.setDefaultCodec ( codec );

			mainWin = new rPem3D();
			if(f) mainWin->clearEnv();
			app.setMainWidget ( mainWin );
			mainWin->show();

			args->clear();

			// mainWin has WDestructiveClose flag by default, so it will delete itself.
			return app.exec();
		}
	}
}
